% Generation for K-L polynomials
% ------------------------------
function K_L = Zernike_to_K_L(V, ZM)
% V: matrix from singular value decomposition 
% Z: Zernike polynomials with size: length(r) x (j_max-1)
% output: 
% K_L: with size same as Z

idx_l = size(ZM,1); % length(r)
idx_s = size(ZM,2); % j_max-1

%if idx_l < idx_s
%    error('rows in ZM must be larger than coloums in ZM');
%end

%V_temp_pre = zeros(1,idx_s);
%V_temp = zeros(idx_l,idx_s);
K_L = zeros(idx_l, idx_s);
for j = 1:idx_s % extract piston term
    %V_temp_pre = (V(:,j))';
    %V_temp(:,:) = repmat(V_temp_pre,idx_l,1);
    %K_L(:,j) = sum(ZM.*V_temp,2);
    V_temp = V(:,j);
    K_L(:,j) = ZM*V_temp;
end

% end of function
end

